package com.skyline.base.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.base.model.Share;
import com.skyline.base.service.ShareService;
import com.skyline.base.type.ShareType;
import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;
import com.skyline.user.service.PersonalInfoService;

/**
 * 分享模块的Controller
 * 
 * @author Jairus Chan
 * @version 0.1
 * */

@Controller
@RequestMapping("/share")
public class ShareController extends BaseController {

	// private static final Log LOGGER =
	// LogFactory.getLog(ShareController.class);

	@Autowired
	private ShareService shareService;

	@Autowired
	private PersonalInfoService personalInfoService;

	// @Value("${view.share.listshare}")
	// private String listView;
	//
	// @Value("${view.share.viewarticleshare}")
	// private String viewArticleShareView;
	//
	 @Value("${share.listshare.pagesize}")
	 private Integer listSharePageSize;

	/**
	 * 增加分享(AJAX应用)
	 * 
	 * @param request
	 * @param shareType
	 *            分享类型
	 * @param resourceId
	 *            被分享的资源的ID
	 * @return <br>
	 *         0:表示此用户没有分享过此资源<br>
	 *         1:表示此用户已经分享过此资源<br>
	 *         2:表示用户没有登陆
	 */
	// FIXME 不要用数字，令人费解
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody
	Integer addShare(ShareType shareType, Long resourceId) {
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user != null && user.getId() != 0) {
			return shareService.addShare(user.getId(), user.getNickname(), user.getPortrait(),
					shareType, resourceId);
		} else {
			return 2;
		}

	}

	@RequestMapping(value = "/up/{id}", method = RequestMethod.POST)
	public @ResponseBody
	void upShare(@PathVariable Long id) {
		shareService.up(id);
	}

	@RequestMapping(value = "/down/{id}", method = RequestMethod.POST)
	public @ResponseBody
	void downShare(@PathVariable Long id) {
		shareService.down(id);
	}

	@RequestMapping(value = "/list/user/{userId}", method = RequestMethod.GET)
	public ModelAndView listShareByUserId(@PathVariable Long userId, Page page) {
		ModelAndView mav = new ModelAndView();
		User user = personalInfoService.getPersonInfoByUserID(userId);
		if (user == null) {
			// TODO:对user不存在的情况进行处理
			return null;
		} else {
			mav.addObject("OWNER", user);
			page.setSize(listSharePageSize);
			List<Share> shares = shareService.getShareBySharerId(userId, page);
			mav.addObject("SHARES", shares);
			mav.addObject("page", page);
			mav.setViewName(ViewPaths.SHARE_LISTSHARE);
			return mav;
		}
	}

	@RequestMapping(value = "/list/user/{userId}/{shareType}", method = RequestMethod.GET)
	public ModelAndView listShareByUserId(@PathVariable Long userId,
			@PathVariable ShareType shareType, Page page) {
		ModelAndView mav = new ModelAndView();
		User user = personalInfoService.getPersonInfoByUserID(userId);
		if (user == null) {
			// TODO:对user不存在的情况进行处理
			return null;
		} else {
			mav.addObject("OWNER", user);
			page.setSize(listSharePageSize);
			List<Share> shares = shareService.getShareBySharerIdAndType(userId, shareType, page);
			mav.addObject("SHARES", shares);
			mav.addObject("page", page);
			mav.setViewName(ViewPaths.SHARE_LISTSHARE);
			return mav;
		}
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public ModelAndView listShareByUserId(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView();
		Share share = shareService.getShareById(id);
		if (share == null) {
			// TODO:对share不存在的情况进行处理
			return null;
		} else {
			mav.addObject("SHARE", share);
			if (share.getShareType().equals(ShareType.ARTICLE)) {
				mav.setViewName(ViewPaths.SHARE_VIEWARTICLESHARE);
			}
			return mav;
		}
	}

}
